package com.parkinglot.util;

import org.springframework.stereotype.Component;

import java.security.SecureRandom;
import java.util.Random;

@Component
public class RandomsUtil {

    public static int[] Randoms(int number) {

        Random rand = new Random();
        int nu[] = new int[7];   //创建一个7位的数组,主要是保存结果，我需要7个不重复的随机值
        boolean[] bool = new boolean[number + 1];  //+1是因为在下面随机数中我为了避开传入的数，不从0开始，所以+1，如果现在这里不加1，会提示越界
        int randint = 0;
        for (int i = 0; i < 7; i++) {
            do {
                randint = rand.nextInt(number) + 1;  //生成给定的随机数
            } while (bool[randint]);   //是否已经生成数字，    bool[randint] 默认是false，如果状态已经设置为了true，不进入状态，有执行do
            bool[randint] = true;   //状态设置为true
            nu[i] = randint;
        }
        return nu;

    }
    public static String buildRandom() {
        StringBuilder uid = new StringBuilder();
        //产生16位的强随机数
        Random rd = new SecureRandom();
        for (int i = 0; i < 32; i++) {
            //产生0-2的3位随机数
            int type = rd.nextInt(3);
            switch (type){
                case 0:
                    //0-9的随机数
                    uid.append(rd.nextInt(10));
                    break;
                case 1:
                    //ASCII在65-90之间为大写,获取大写随机
                    uid.append((char)(rd.nextInt(25)+65));
                    break;
                case 2:
                    //ASCII在97-122之间为小写，获取小写随机
                    uid.append((char)(rd.nextInt(25)+97));
                    break;
                default:
                    break;
            }
        }
        return uid.toString();
    }

    public static void main(String[] args) {
        System.out.println(buildRandom()+"-----------------------------------------");
    }
}
